var app = require('AppJsp');
var GlobalConfig = require('Global');

cc.Proxy = (function () {

    var isCallLogin = false;
    var isEnterHome = false;
    var isCheckNet = false;
    GlobalConfig(JSON.parse(app.getAppConfig()));

    return {

        callLogin() {
            if (cc.GameProxy) {
                cc.GameProxy.callLogin();
            } else {
                if (isCallLogin) return;
                isEnterHome = true;
                isCallLogin = true;
                cc.Toast.loading(true);
                setTimeout(() => app.callLogin(), 500);
            }
        },

        getToken(cbk) {
            var loginUrl = CONFIG.baseUrl + '/api_platform/api/user/login';
            var tokenUrl = CONFIG.baseUrl + '/api_platform/api/user/accessToken';
            cc.Linker(loginUrl, {
                username: CONFIG.username,
                password: '123456'
            }).request(token => {
                cc.Linker(tokenUrl, {
                    type: 1,
                    token: token
                }).request(data => {
                    data.token = data.accessToken;
                    data.userSource = 0;
                    cbk && cbk(data);
                });
            });
        },

        openUrl(url) {
            app.openGame(url);
        },

        openGame(game) {
            if (game[game.length - 1] == '/') game += CONFIG.language;
            else game += '/' + CONFIG.language;
            LINK.accessToken ? app.openGame(game) : app.callLogin();
        },

        appLog(data) {
            app.appLog(data);
        },

        checkNetwork() {
            cc.Linker(URL.GameList).showLoading(false).request(() => {
                if (isCheckNet) {
                    isCheckNet = false;
                    cc.Toast.cancelLoading();
                    cc.Emitter.emit(EVENTS.GAME_RESUME);
                }
            }, (msg, code) => {
                isCheckNet = true;
                cc.Toast.loading(true);
                setTimeout(() => this.checkNetwork(), 1000);
            });
        },

        gameCharge(data) {
            var d = null;
            if (data.bizTarget) {
                d = {
                    GameOrderNumber: data,
                    GameRechargeMoney: data.price,
                    productId: data.thirdId,
                    symbol: data.symbol,
                    name: data.name,
                    id: data.id
                }
            } else {
                d = {
                    GameOrderNumber: null,
                    GameRechargeMoney: data.amount,
                    productId: data.thirdId,
                    symbol: data.symbol,
                    name: data.label,
                    id: data.id
                }
            }
            cc.Toast.loading(true);
            if (cc.GameProxy) {
                cc.GameProxy.gameCharge(d);
            } else {
                app.gameCharge(JSON.stringify(d));
            }
        },

        userBind() {
            if (cc.GameProxy) {
                cc.GameProxy.userBind();
            } else {
                app.userBind();
            }
        },

        userLogout() {
            app.onLogout();
            LINK.userInfo = null;
            LINK.accessToken = null;
            isCallLogin = true;
            cc.Toast.loading(true);
            cc.Emitter.emit(EVENTS.USER_LOGOUT);
        },

        buryingPoint(type, cbk) {
            cc.Linker(URL.BuryingPoint, {
                buryingType: type,
                gameType: 0,
            }).showMessage(false).showLoading(false).request(cbk, cbk);
        },
        newburyingPoint(eventId,param,cbk){
            var params =  param ? param : " ";
            cc.Linker(URL.UserActonPoint,{code:eventId,param:params}).showLoading(false).showMessage(false).request(data => {
             
            });
         },
        refreshAmount() {
            cc.Linker(URL.UserInfo).showLoading(false).showMessage(false).request(data => {
                LINK.userInfo = data;
                cc.Emitter.emit(EVENTS.REFRESH_AMOUNT);
            });
        },

        refreshEmailAmount() {
            cc.Linker(URL.GetEmailAmount).showLoading(false).showMessage(false).request(data => {
                cc.Emitter.emit(EVENTS.EMAIL_AMOUNT_REFRESH, data);
            });
        },
        ////////////called by app/////////////
        onUserLogin(data) {
            isCallLogin = false;
            if (!data) {
                this.callLogin();
                return;
            }
            if (data.token != LINK.accessToken) {
                cc.Toast.loading(false);
                LINK.accessToken = data.token;
                LINK.isGuestLogin = data.userSource == 0;
                cc.Emitter.emit(EVENTS.GET_TOKEN);
            }
        },

        onGameRecharge(data) {
            cc.Toast.loading(false);
            if (data.state == 1) {
                this.refreshAmount();
                cc.Emitter.emit(EVENTS.GAME_RECHARGE, data.orderId);
            }
        },

        onGameResume() {
            if (!isEnterHome || isCallLogin || isCheckNet) return;
            if (LINK.accessToken) {
                this.checkNetwork();
                cc.Proxy.newburyingPoint(7014);
                cc.Emitter.emit(EVENTS.GAME_RESUME);
            }
        }
    }
})();